package zengjin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import zengjin.common.BeanCopyUtil;
import zengjin.common.PageResponse;
import zengjin.mapper.ImageMapper;
import zengjin.mapper.ResumeMapper;
import zengjin.model.dto.resume.AddResumeRequest;
import zengjin.model.dto.resume.PageResumeRequest;
import zengjin.model.dto.resume.UpdateResumeRequest;
import zengjin.model.en.Image;
import zengjin.model.en.Resume;
import zengjin.model.vo.resume.ResumeVO;
import zengjin.service.ImageService;
import zengjin.service.ResumeService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 19324
 * @description 针对表【resume】的数据库操作Service
 * @createDate 2025-02-11 21:53:38
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume>
        implements ResumeService {
    @Resource
    private ResumeMapper resumeMapper;

    @Resource
    private ImageMapper imageMapper;

    @Resource
    private ImageService imageService;

    @Override
    public Boolean addResume(AddResumeRequest addResumeRequest) {
        //1.获取参数
        Long resumeJobseekerId = addResumeRequest.getResumeJobseekerId();
        String resumeAvatar = addResumeRequest.getResumeAvatar();
        String resumeUsername = addResumeRequest.getResumeUsername();
        String resumeGender = addResumeRequest.getResumeGender();
        Integer resumeAge = addResumeRequest.getResumeAge();
        String resumePhone = addResumeRequest.getResumePhone();
        String resumeAddress = addResumeRequest.getResumeAddress();
        String resumeEducationSchool = addResumeRequest.getResumeEducationSchool();
        String resumeEducationMajor = addResumeRequest.getResumeEducationMajor();
        String resumeEducationDegree = addResumeRequest.getResumeEducationDegree();
        String resumeSchoolExperience = addResumeRequest.getResumeSchoolExperience();
        Date resumeEducationEndDate = addResumeRequest.getResumeEducationEndDate();
        String resumeWorkExperience = addResumeRequest.getResumeWorkExperience();
        String resumeSkillDescription = addResumeRequest.getResumeSkillDescription();
        String resumeCertificatesVerified = addResumeRequest.getResumeCertificatesVerified();
        String resumeCertificates = addResumeRequest.getResumeCertificates();
        String resumePersonalProfile = addResumeRequest.getResumePersonalProfile();
        String resumePrivacySetting = addResumeRequest.getResumePrivacySetting();
        String resumeExpectedJob = addResumeRequest.getResumeExpectedJob();
        String resumeExpectedWorkAddress = addResumeRequest.getResumeExpectedWorkAddress();
        Integer resumeExpectedSalary = addResumeRequest.getResumeExpectedSalary();
        String resumeExpectedSalaryType = addResumeRequest.getResumeExpectedSalaryType();

        //2.创建Resume对象
        Resume resume = new Resume();
        resume.setResumeJobseekerId(resumeJobseekerId);
        resume.setResumeUsername(resumeUsername);
        resume.setResumeGender(resumeGender);
        resume.setResumeAge(resumeAge);
        resume.setResumePhone(resumePhone);
        resume.setResumeAddress(resumeAddress);
        resume.setResumeEducationSchool(resumeEducationSchool);
        resume.setResumeEducationMajor(resumeEducationMajor);
        resume.setResumeEducationDegree(resumeEducationDegree);
        resume.setResumeSchoolExperience(resumeSchoolExperience);
        resume.setResumeEducationEndDate(resumeEducationEndDate);
        resume.setResumeWorkExperience(resumeWorkExperience);
        resume.setResumeSkillDescription(resumeSkillDescription);
        resume.setResumeCertificatesVerified(resumeCertificatesVerified);
        resume.setResumeCertificates(resumeCertificates);
        resume.setResumePersonalProfile(resumePersonalProfile);
        resume.setResumePrivacySetting(resumePrivacySetting);
        resume.setResumeExpectedJob(resumeExpectedJob);
        resume.setResumeExpectedWorkAddress(resumeExpectedWorkAddress);
        resume.setResumeExpectedSalary(resumeExpectedSalary);
        resume.setResumeExpectedSalaryType(resumeExpectedSalaryType);

        int insert = resumeMapper.insert(resume);
        int update = 0;
        if (insert > 0) {
            Long resumeId = resume.getResumeId();
            //3.插入头像信息
            if (StrUtil.isNotBlank(resumeAvatar)) {
                Image image = new Image();
                image.setImageName("resume_avatar");
                image.setImageUrl(resumeAvatar);
                image.setImageNameId(resumeId);
                update = imageMapper.insert(image);
            }
        }
        //4.调用方法
        return (insert + update) > 0;
    }

    @Override
    public ResumeVO getResumeDetailByJobseekerId(Long resumeJobseekerId) {
        //1.构建查询条件
        QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("resume_jobseeker_id", resumeJobseekerId);
        Resume resume = resumeMapper.selectOne(queryWrapper);
        if(resume == null){return null;}
        ResumeVO resumeVO = BeanUtil.copyProperties(resume, ResumeVO.class);
        //2.查询出头像
        QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
        imageQueryWrapper.eq("image_name", "resume_avatar");
        imageQueryWrapper.eq("image_name_id", resume.getResumeId());
        Image image = imageMapper.selectOne(imageQueryWrapper);
        if (image != null) {
            resumeVO.setResumeAvatar(image.getImageUrl());
        }
        return resumeVO;
    }

    @Override
    public Boolean updateResumeById(UpdateResumeRequest updateResumeRequest) {
        //1.获取参数
        Long resumeId = updateResumeRequest.getResumeId();
        String resumeAvatar = updateResumeRequest.getResumeAvatar();
        String resumeUsername = updateResumeRequest.getResumeUsername();
        String resumeGender = updateResumeRequest.getResumeGender();
        Integer resumeAge = updateResumeRequest.getResumeAge();
        String resumePhone = updateResumeRequest.getResumePhone();
        String resumeAddress = updateResumeRequest.getResumeAddress();
        String resumeEducationSchool = updateResumeRequest.getResumeEducationSchool();
        String resumeEducationMajor = updateResumeRequest.getResumeEducationMajor();
        String resumeEducationDegree = updateResumeRequest.getResumeEducationDegree();
        String resumeSchoolExperience = updateResumeRequest.getResumeSchoolExperience();
        Date resumeEducationEndDate = updateResumeRequest.getResumeEducationEndDate();
        String resumeWorkExperience = updateResumeRequest.getResumeWorkExperience();
        String resumeSkillDescription = updateResumeRequest.getResumeSkillDescription();
        String resumeCertificates = updateResumeRequest.getResumeCertificates();
        String resumePersonalProfile = updateResumeRequest.getResumePersonalProfile();
        String resumePrivacySetting = updateResumeRequest.getResumePrivacySetting();
        String resumeExpectedJob = updateResumeRequest.getResumeExpectedJob();
        String resumeExpectedWorkAddress = updateResumeRequest.getResumeExpectedWorkAddress();
        Integer resumeExpectedSalary = updateResumeRequest.getResumeExpectedSalary();
        String resumeExpectedSalaryType = updateResumeRequest.getResumeExpectedSalaryType();

        //2.更新简历信息
        LambdaUpdateWrapper<Resume> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Resume::getResumeId, resumeId)
                .set(StrUtil.isNotBlank(resumeUsername), Resume::getResumeUsername, resumeUsername)
                .set(StrUtil.isNotBlank(resumeGender), Resume::getResumeGender, resumeGender)
                .set(resumeAge != null, Resume::getResumeAge, resumeAge)
                .set(StrUtil.isNotBlank(resumePhone), Resume::getResumePhone, resumePhone)
                .set(StrUtil.isNotBlank(resumeAddress), Resume::getResumeAddress, resumeAddress)
                .set(StrUtil.isNotBlank(resumeEducationSchool), Resume::getResumeEducationSchool, resumeEducationSchool)
                .set(StrUtil.isNotBlank(resumeEducationMajor), Resume::getResumeEducationMajor, resumeEducationMajor)
                .set(StrUtil.isNotBlank(resumeEducationDegree), Resume::getResumeEducationDegree, resumeEducationDegree)
                .set(StrUtil.isNotBlank(resumeSchoolExperience), Resume::getResumeSchoolExperience, resumeSchoolExperience)
                .set(resumeEducationEndDate != null, Resume::getResumeEducationEndDate, resumeEducationEndDate)
                .set(StrUtil.isNotBlank(resumeWorkExperience), Resume::getResumeWorkExperience, resumeWorkExperience)
                .set(StrUtil.isNotBlank(resumeSkillDescription), Resume::getResumeSkillDescription, resumeSkillDescription)
                .set(StrUtil.isNotBlank(resumeCertificates), Resume::getResumeCertificates, resumeCertificates)
                .set(StrUtil.isNotBlank(resumePersonalProfile), Resume::getResumePersonalProfile, resumePersonalProfile)
                .set(StrUtil.isNotBlank(resumePrivacySetting), Resume::getResumePrivacySetting, resumePrivacySetting)
                .set(StrUtil.isNotBlank(resumeExpectedJob), Resume::getResumeExpectedJob, resumeExpectedJob)
                .set(StrUtil.isNotBlank(resumeExpectedWorkAddress), Resume::getResumeExpectedWorkAddress, resumeExpectedWorkAddress)
                .set(resumeExpectedSalary != null, Resume::getResumeExpectedSalary, resumeExpectedSalary)
                .set(StrUtil.isNotBlank(resumeExpectedSalaryType), Resume::getResumeExpectedSalaryType, resumeExpectedSalaryType);

        //3.更新头像信息
        int update = 0;
        if (StrUtil.isNotBlank(resumeAvatar)) {
            //查询出头像
            QueryWrapper<Image> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("image_name_id", resumeId)
                    .eq("image_name", "resume_avatar");
            Image image = imageMapper.selectOne(queryWrapper);
            if (image != null && (!image.getImageUrl().equals(resumeAvatar))) {
                //先删除原来的头像
                String name = image.getImageUrl();
                imageService.delete(name);
                //更新新的头像
                image.setImageUrl(resumeAvatar);
                update = imageMapper.updateById(image);

            } else if (image == null) {
                image = new Image();
                //插入新的头像
                image.setImageName("resume_avatar");
                image.setImageUrl(resumeAvatar);
                image.setImageNameId(resumeId);
                update = imageMapper.insert(image);
            }
        }
        return (resumeMapper.update(null, updateWrapper) + update) > 0;
    }

    @Override
    public boolean updateResumeVerified(Long resumeId, String resumeCertificatesVerified) {
        //1.构造条件
        LambdaUpdateWrapper<Resume> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Resume::getResumeId, resumeId)
                .set(Resume::getResumeCertificatesVerified, resumeCertificatesVerified);
        //2.更新
        return resumeMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public PageResponse<ResumeVO> pageResume(PageResumeRequest pageResumeRequest) {
        //1.获取参数
        String resumePrivacySetting = pageResumeRequest.getResumePrivacySetting();

        //2.构造条件
        Page<Resume> page = pageResumeRequest.toPageDefaultSortByUpdateTime();

        QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(resumePrivacySetting), "resume_privacy_setting", resumePrivacySetting);
        List<Resume> resumes = resumeMapper.selectList(queryWrapper);
        List<ResumeVO> resumeVOS = new ArrayList<>();
        resumes.forEach(resume -> {
            //查询头像
            ResumeVO resumeVO = BeanUtil.copyProperties(resume, ResumeVO.class);
            QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
            imageQueryWrapper.eq("image_name", "resume_avatar");
            imageQueryWrapper.eq("image_name_id", resume.getResumeId());
            Image image = imageMapper.selectOne(imageQueryWrapper);
            if (image != null) {
                resumeVO.setResumeAvatar(image.getImageUrl());
            }
            resumeVOS.add(resumeVO);
        });

        return PageResponse.of(page, resumeVOS);
    }
}
